package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.BedroomGlory;
import com.teacher.pojo.entity.BedroomLevel;
import com.teacher.pojo.enums.BedroomLevelType;
import com.teacher.pojo.excel.BedroomGloryExcel;
import com.teacher.pojo.excel.BedroomLevelExcel;
import com.teacher.service.IBedroomGloryService;
import com.teacher.service.IBedroomLevelService;
import com.teacher.service.IPerStudentService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelBedroomGloryListener extends AnalysisEventListener<BedroomGloryExcel> {
    private Long termId;
    private IBedroomGloryService iBedroomGloryService;

    private List<BedroomGlory> bedroomGloryList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelBedroomGloryListener(Long termId, IBedroomGloryService iBedroomGloryService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iBedroomGloryService = iBedroomGloryService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(BedroomGloryExcel bedroomGloryExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(bedroomGloryExcel.getStudentKey())) {
            BedroomGlory bedroomGlory = new BedroomGlory();
            BeanUtils.copyProperties(bedroomGloryExcel, bedroomGlory);
            bedroomGlory.setTermId(termId);
            bedroomGloryList.add(bedroomGlory);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iBedroomGloryService.saveBatch(bedroomGloryList);
    }
}
